package org.mspring.search.listener;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;
import org.hibernate.persister.entity.EntityPersister;
import org.mspring.search.task.LuceneUpdateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Gao Youbo
 * @since 2013年8月1日
 */
@Component
public class LuceneUpdateEventListener implements PostUpdateEventListener {

    private static final long serialVersionUID = -2491193362753498052L;

    @Autowired
    private LuceneUpdateTask luceneUpdateTask;

    @Override
    public void onPostUpdate(PostUpdateEvent event) {

        try {
            Map<Object, Object> context = new HashMap<Object, Object>();
            context.put("searchable", event.getEntity());
            luceneUpdateTask.doAsyncTask(context);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    public boolean requiresPostCommitHanding(EntityPersister arg0) {

        return false;
    }
}
